package com.base.cn.platform.os.config.scheduled;

import com.base.cn.platform.os.service.manage.statistics.ZStatisticsActiveUserCountService;
import com.base.cn.platform.os.service.manage.statistics.ZStatisticsPageViewCountService;
import com.base.cn.platform.os.service.manage.statistics.ZStatisticsUniqueVisitorCountService;
import com.base.cn.platform.os.service.manage.statistics.flow.ZStatisticsTerminalUVCountService;
import com.base.cn.platform.os.service.manage.statistics.goods.ZStatisticsGoodsPVCountService;
import com.base.cn.platform.os.service.manage.statistics.goods.ZStatisticsGoodsUVCountService;
import com.base.cn.platform.os.service.manage.statistics.goods.ZStatisticsSubjectStudyUVCountService;
import com.base.cn.platform.os.statistics.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务
 *
 * @author s.li
 * @date 2019/04/17
 */
@Component
@Slf4j
public class ScheduledTask {
    @Autowired
    private ZStatisticsPageViewCountService zStatisticsPageViewCountService;
    @Autowired
    private ZStatisticsUniqueVisitorCountService zStatisticsUniqueVisitorCountService;
    @Autowired
    private ZStatisticsActiveUserCountService zStatisticsActiveUserCountService;
    @Autowired
    private ZStatisticsGoodsUVCountService zStatisticsGoodsUVCountService;
    @Autowired
    private ZStatisticsGoodsPVCountService zStatisticsGoodsPVCountService;
    @Autowired
    private ZStatisticsSubjectStudyUVCountService zStatisticsSubjectStudyUVCountService;
    @Autowired
    private ZStatisticsTerminalUVCountService zStatisticsTerminalUVCountService;

    @Scheduled(cron = "0 1/30 * * * ?") //每个小时的第3分钟和第31分钟执行一次
    public void pvTask(){
        log.info("----------------------pvTask PV定时器任务，每个小时的第1分钟和第31分钟执行一次");
        //执行PV统计数据
        new Thread(new PVTask(zStatisticsPageViewCountService)).start();
    }

    @Scheduled(cron = "0 3/30 * * * ?") //每个小时的第3分钟和第33分钟执行一次
    public void uvTask(){
        log.info("----------------------uvTask UV定时器任务，每个小时的第3分钟和第33分钟执行一次");
        //执行UV统计数据
        new Thread(new UVTask(zStatisticsUniqueVisitorCountService)).start();
    }

    @Scheduled(cron = "0 6/30 * * * ? ") //每个小时的第6分钟和第36分钟执行一次
    //@Scheduled(cron = "0/10 * * * * *")
    public void uaTask(){
        log.info("----------------------uaTask UA定时器任务，每个小时的第6分钟和第36分钟执行一次");
        new Thread(new UATask(zStatisticsActiveUserCountService)).start();
    }

    @Scheduled(cron = "0 9/30 * * * ?") //每个小时的第9分钟和第39分钟执行一次
    //@Scheduled(cron = "0/10 * * * * *")
    public void goodsPVUVTask(){
        log.info("----------------------goodsPVUVTask UV定时器任务，每个小时的第9分钟和第39分钟执行一次");
        new Thread(new GoodsPVUVTask(zStatisticsGoodsUVCountService,zStatisticsGoodsPVCountService)).start();
    }

    @Scheduled(cron = "0 12/30 * * * ?") //每个小时的第12分钟和第42分钟执行一次
    public void subjectStudyUVTask(){
        log.info("----------------------subjectStudyUVTask UV定时器任务，每个小时的第12分钟和第42分钟执行一次");
        new Thread(new SubjectStudyUVTask(zStatisticsSubjectStudyUVCountService)).start();
    }

    @Scheduled(cron = "0 15/30 * * * ?")//每个小时的第15分钟和第45分钟执行一次
    public void terminalUVTask(){
        log.info("----------------------terminalUVTask UV定时器任务，每个小时的第15分钟和第45分钟执行一次");
        new Thread(new TerminalUVTask(zStatisticsTerminalUVCountService)).start();
    }

    /**
     * 执行PV统计数据任务
     */
    class PVTask implements Runnable{
        private ZStatisticsPageViewCountService zStatisticsPageViewCountService;
        public PVTask(ZStatisticsPageViewCountService zStatisticsPageViewCountService){
            this.zStatisticsPageViewCountService = zStatisticsPageViewCountService;
        }

        @Override
        public void run() {
            PVStatisticsUtils.pvStatistics(zStatisticsPageViewCountService);
        }
    }

    /**
     * 执行UV统计数据任务
     */
    class UVTask implements Runnable{
        private ZStatisticsUniqueVisitorCountService zStatisticsUniqueVisitorCountService;

        public UVTask(ZStatisticsUniqueVisitorCountService zStatisticsUniqueVisitorCountService){
            this.zStatisticsUniqueVisitorCountService = zStatisticsUniqueVisitorCountService;
        }

        @Override
        public void run() {
            UVStatisticsUtils.uvStatistics(zStatisticsUniqueVisitorCountService);
        }
    }

    /**
     * 执行UA统计数据任务
     */
    class UATask implements Runnable{
        private ZStatisticsActiveUserCountService zStatisticsActiveUserCountService;

        public UATask(ZStatisticsActiveUserCountService zStatisticsActiveUserCountService){
            this.zStatisticsActiveUserCountService = zStatisticsActiveUserCountService;
        }

        @Override
        public void run() {
            UAStatisticsUtils.uaStatistics(zStatisticsActiveUserCountService);
        }
    }

    /**
     * 商品PV/UV统计任务
     */
    class GoodsPVUVTask implements Runnable{
        private ZStatisticsGoodsUVCountService zStatisticsGoodsUVCountService;
        private ZStatisticsGoodsPVCountService zStatisticsGoodsPVCountService;
        public GoodsPVUVTask(ZStatisticsGoodsUVCountService zStatisticsGoodsUVCountService,
                             ZStatisticsGoodsPVCountService zStatisticsGoodsPVCountService){
            this.zStatisticsGoodsUVCountService = zStatisticsGoodsUVCountService;
            this.zStatisticsGoodsPVCountService = zStatisticsGoodsPVCountService;
        }
        @Override
        public void run() {
            GoodsUVStatisticsUtils.goodsPVUVStatistics(zStatisticsGoodsUVCountService,zStatisticsGoodsPVCountService);
        }
    }

    /**
     * 专业学习UV统计任务
     */
    class SubjectStudyUVTask implements Runnable{
        private ZStatisticsSubjectStudyUVCountService zStatisticsSubjectStudyUVCountService;

        public SubjectStudyUVTask(ZStatisticsSubjectStudyUVCountService zStatisticsSubjectStudyUVCountService){
            this.zStatisticsSubjectStudyUVCountService = zStatisticsSubjectStudyUVCountService;
        }
        @Override
        public void run() {
            SubjectStudyUtils.subjectStudyUVStatistics(zStatisticsSubjectStudyUVCountService);
        }
    }

    /**
     * 设置UV统计任务
     */
    class TerminalUVTask implements Runnable{
        private ZStatisticsTerminalUVCountService terminalUVCountService;

        public TerminalUVTask(ZStatisticsTerminalUVCountService terminalUVCountService){
            this.terminalUVCountService = terminalUVCountService;
        }
        @Override
        public void run() {
            TerminalUtils.terminalUVStatistics(terminalUVCountService);
        }
    }
}
